<?php

namespace App\Listeners\Cruise\CruiseOrder;

use App\Events\Cruise\CruiseOrder\CruiseOrderStatusChangeEvent;
use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseOrderProcessModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Remote\Rms\RmsUserRemote;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;

class CruiseOrderStatusChangeListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param CruiseOrderStatusChangeEvent $event
     * @return void
     * @throws GuzzleException
     */
    public function handle(CruiseOrderStatusChangeEvent $event)
    {
        try {
            Log::info("门店自检状态变化监听事件 : cruiseOrderId: {$event->cruiseOrderId} status: {$event->status}");
            $cruiseCheckSelfDetailModel = CruiseCheckSelfDetailModel::query()
                                                                    ->where("templateType", CruiseTemplateModel::TYPE_8)
                                                                    ->where("cruiseOrderId", $event->cruiseOrderId)
                                                                    ->first();

            if (!$cruiseCheckSelfDetailModel) {
                throw new \Exception("门店自检状态变化监听事件失败 cruiseOrderId: {$event->cruiseOrderId} status: $event->status 查询记录失败");
            }

            // 表单状态扭转 监听事件
            // 同步自检任务表状态
            switch ($event->status) {
                case CruiseOrderModel::STATUS_NOT_REVIEW:// 已提交
                    $submit = CruiseOrderProcessModel::query()
                                                     ->where("cruiseOrderId", $event->cruiseOrderId)
                                                     ->where("step", CruiseOrderProcessModel::PROCESS_SUBMITTED)
                                                     ->orderByDesc("id")
                                                     ->first();

                    // 更新表单提交人数据
                    if ($submit) {
                        $submitUser = RmsUserRemote::getRmsUserByIds([$submit->createdByUid],
                                ['id', 'username'])[0] ?? [];

                        // 单据状态变为已提交
                        CruiseCheckSelfDetailModel::query()->where("cruiseOrderId", $event->cruiseOrderId)->update([
                            "status"         => CruiseCheckSelfDetailModel::STATUS_4,
                            "submitUid"      => attributeGet($submit, "id", 0),
                            "submitUsername" => attributeGet($submitUser, "username", ""),
                            "submitTime"     => $submit->createdAt->timestamp
                        ]);
                    }
                    break;
                case CruiseOrderModel::STATUS_AUDIT_OK: // 审核正常
                    // 单据状态变为审核正常
                    CruiseCheckSelfDetailModel::query()->where("cruiseOrderId", $event->cruiseOrderId)->update([
                        "status"     => CruiseCheckSelfDetailModel::STATUS_10,
                        "finishTime" => time()
                    ]);
                    break;
                case CruiseOrderModel::STATUS_AUDIT_ERROR: // 审核异常
                    // 单据状态变为审核异常
                    CruiseCheckSelfDetailModel::query()->where("cruiseOrderId", $event->cruiseOrderId)->update([
                        "status"     => CruiseCheckSelfDetailModel::STATUS_9,
                        "finishTime" => time()
                    ]);
                    break;
            }
        } catch (\Exception $exception) {
            Log::error("门店自检状态变化监听事件失败 : cruiseOrderId: {$event->cruiseOrderId} status: $event->status ", [
                "msg"   => $exception->getMessage(),
                "code"  => $exception->getCode(),
                "file"  => $exception->getFile(),
                "line"  => $exception->getLine(),
                "trace" => $exception->getTraceAsString()
            ]);
        }

    }
}
